/**********************************************************************
  The MIT License (MIT)
  Copyright (c) 2012 sunriseyuen

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files
  (the "Software"), to deal in the Software without restriction,
  including without limitation the rights to use, copy, modify, merge,
  publish, distribute, sublicense, and/or sell copies of the Software,
  and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
  THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ***********************************************************************/

	#define _DEF_CCY_CODE
	#define _ZZDEBUG
	#define _PROJ_DC

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

#region Using directives
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;

using Muki.Framework.Util;
using Muki.Framework.Business;
using Muki.Framework;
using Muki.UserDefine;
using Muki.Entity;
using Dot.StreamObject;

#endregion

namespace Muki.UserPanel
{
	public class UID02001BM:BLogic
	{
		public UID02001BM()
		{
			objDefault.UID_CODE    ="UID02001B";
		}

		public void Initialize()
		{
			objDefault.ZZLOGON_SSD = this.ZZLOGON_SSD;
			objDefault.Trans       = this.Trans;
			objDefault.ZZLOGON_ID  = this.ZZLOGON_ID;

			_Convert.ZZLOGON_SSD = this.ZZLOGON_SSD;
			_Convert.Trans       = this.Trans;

			_DataPermission.ZZLOGON_SSD = this.ZZLOGON_SSD;
			_DataPermission.Trans       = this.Trans;
			_DataPermission.TABLE_NAME  = "ZUPRGDTM";
			_DataPermission.ZZLOGON_ID  = this.ZZLOGON_ID;
			_DataPermission.DataAccessRight();

			_DocsPermission.ZZLOGON_SSD  = this.ZZLOGON_SSD;
			_DocsPermission.Trans        = this.Trans;

			_Setting.ZZLOGON_ID = this.ZZLOGON_ID;
			_Setting.Trans      = this.Trans;

		}

		public UID02001BE Verify(UID02001BE _Entity)
		{

						_Entity.UID_DT=_Entity.UID_DT.Trim();


























				return _Entity;
		}

		public List<UID02001BE> Verify(List<UID02001BE> _Entitys)
		{
			List<UID02001BE> _New_Entitys=new List<UID02001BE>();

			foreach (UID02001BE _Entity in _Entitys) 
			{
				_New_Entitys.Add(Verify(_Entity));
			}
			return _New_Entitys;
		}

		public UID02001BE Default(UID02001BE _Entity)
		{

						_Entity.UID_DT=objDefault.getDefault("UID_DT",_Entity.UID_DT);
						_Entity.SERIAL_NO=objDefault.getDefault("SERIAL_NO",_Entity.SERIAL_NO);
						_Entity.TABLE_NAME=objDefault.getDefault("TABLE_NAME",_Entity.TABLE_NAME);
						_Entity.COLUMN_NAME=objDefault.getDefault("COLUMN_NAME",_Entity.COLUMN_NAME);
						_Entity.COLUMN_NULLABLE=objDefault.getDefault("COLUMN_NULLABLE",_Entity.COLUMN_NULLABLE);
						_Entity.LABEL_ID=objDefault.getDefault("LABEL_ID",_Entity.LABEL_ID);
						_Entity.UID_ENG_NAME=objDefault.getDefault("UID_ENG_NAME",_Entity.UID_ENG_NAME);
						_Entity.UID_CHS_NAME=objDefault.getDefault("UID_CHS_NAME",_Entity.UID_CHS_NAME);
						_Entity.BASE_COLUMN_NAME=objDefault.getDefault("BASE_COLUMN_NAME",_Entity.BASE_COLUMN_NAME);
						_Entity.BASE_TABLE_NAME=objDefault.getDefault("BASE_TABLE_NAME",_Entity.BASE_TABLE_NAME);
						_Entity.LNK_COLUMN_NAME=objDefault.getDefault("LNK_COLUMN_NAME",_Entity.LNK_COLUMN_NAME);
						_Entity.OPT_CODE=objDefault.getDefault("OPT_CODE",_Entity.OPT_CODE);
						_Entity.RELY_VERFY=objDefault.getDefault("RELY_VERFY",_Entity.RELY_VERFY);
						_Entity.RELY_DEFAULT=objDefault.getDefault("RELY_DEFAULT",_Entity.RELY_DEFAULT);
						_Entity.RELY_VIEW=objDefault.getDefault("RELY_VIEW",_Entity.RELY_VIEW);
						_Entity.NEW_LINE=objDefault.getDefault("NEW_LINE",_Entity.NEW_LINE);
						_Entity.NEW_CELL=objDefault.getDefault("NEW_CELL",_Entity.NEW_CELL);
						_Entity.ROWS_SPAN=objDefault.getDefault("ROWS_SPAN",_Entity.ROWS_SPAN);
						_Entity.COLS_SPAN=objDefault.getDefault("COLS_SPAN",_Entity.COLS_SPAN);
						_Entity.ACTIVE_BIT=objDefault.getDefault("ACTIVE_BIT",_Entity.ACTIVE_BIT);
						_Entity.LINE_REVEAL=objDefault.getDefault("LINE_REVEAL",_Entity.LINE_REVEAL);



				return _Entity;
		}

		public List<UID02001BE> Default(List<UID02001BE> _Entitys)
		{
			List<UID02001BE> _New_Entitys=new List<UID02001BE>();
			foreach (UID02001BE _Entity in _Entitys) 
			{
				_New_Entitys.Add(Default(_Entity));
			}
			return _New_Entitys;
		}

		public UID02001BE Search(VContexts _VContexts,UID02001BE _Entity)
		{


			if (_DocsPermission.AccessRight("UID02001B", this.ZZLOGON_ID, "ACCESS")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02001B","NotAccessRight", this.ZZLOGON_ID));
				return _Entity;
			}
		
			ZZSQLStatement  _SQLStatement = new ZZSQLStatement();

			_SQLStatement.Trans         = this.Trans;
			_SQLStatement.UID_CODE      = "UID02001B";
			_SQLStatement.ZZLOGON_SSD   = this.ZZLOGON_SSD;
			_SQLStatement.ZZLOGON_LANG  = this.ZZLOGON_LANG;
			_SQLStatement.DbName        = this.DbName;
			_SQLStatement.Process("QID02001B");

			ZZQLite oZZQLite = _SQLStatement.oZZQLite;

			if (_VContexts.getValue("T1")!="" && _VContexts.getValue("V1")!="")
			{
				oZZQLite.AddXWhereClause("", _VContexts.getValue("T1"), _VContexts.getValue("V1"));
			}
			if (_VContexts.getValue("T2")!="" && _VContexts.getValue("V2")!="")
			{
				oZZQLite.AddXWhereClause("", _VContexts.getValue("T2"), _VContexts.getValue("V2"));
			}
			if (_VContexts.getValue("T3")!="" && _VContexts.getValue("V3")!="")
			{
				oZZQLite.AddXWhereClause("", _VContexts.getValue("T3"), _VContexts.getValue("V3"));
			}

			if (!Util.IsNullOrEmpty(_VContexts.getValue("LOOKUP_DATA")))
			{
					oZZQLite.AddXWhereClause("ZZLOOKUP_ANY_COLUMN", ";ZUPRGDTM.UID_CODE;ZUPRGDTM.UID_DT;ZUPRGDTM.TABLE_NAME;ZUPRGDTM.COLUMN_NAME;ZUPRGDTM.LABEL_ID;ZUPRGDTM.BASE_COLUMN_NAME;ZUPRGDTM.BASE_TABLE_NAME;ZUPRGDTM.LNK_COLUMN_NAME;ZUPRGDTM.OPT_CODE;ZUPRGDTM.RELY_VERFY;ZUPRGDTM.RELY_DEFAULT;ZUPRGDTM.RELY_VIEW", _VContexts.getValue("LOOKUP_DATA"));
			}

						if (!Util.IsNullOrEmpty(_Entity.UID_CODE))
						{
							_VContexts.setValue("TT1","ZUPRGDTM.UID_CODE");
							_VContexts.setValue("VV1",_Entity.UID_CODE);
							oZZQLite.AddWhereClause("ZUPRGDTM", "UID_CODE", _Entity.UID_CODE);
						}

			oZZQLite.Condition=_VContexts.getValue("Condition");
			oZZQLite.Make();

			int _AbsolutePage = _VContexts.getInteger("AbsolutePage");
			int _PageSize     = _VContexts.getInteger("ZZPageSize");
			int _Restrict     = _VContexts.getInteger("Restrict");
			int _TotalPages   = 0;

			_Setting.Trans      = this.Trans;

			if (_Restrict == 0)
			{
				_Restrict=30;
			}
	
			if (_PageSize <= 0)
			{
				_PageSize=_Setting.getInteger("ZZPageSize","20");
			}

			QueryRows RsZUPRGDTM   = new QueryRows(this.Trans);
			RsZUPRGDTM.CommandText = oZZQLite.SQLStatement;
			RsZUPRGDTM.Top         = _Restrict;
			RsZUPRGDTM.Skip        = (_AbsolutePage-1)*_PageSize;
			RsZUPRGDTM.Limit       = _PageSize;
			RsZUPRGDTM.CountAll    = true;
			RsZUPRGDTM.Open();

			if (_PageSize <= 0)
			{
				_TotalPages   = RsZUPRGDTM.RecordCount;
				_PageSize = RsZUPRGDTM.RecordCount;
			}
			else
			{
				_TotalPages= RsZUPRGDTM.RecordCount / _PageSize;
			}
			if (RsZUPRGDTM.RecordCount > (RsZUPRGDTM.RecordCount / _PageSize) * _PageSize)
			{
				_TotalPages = _TotalPages+ 1;
			}

			if (_AbsolutePage <= 0)
			{
				_AbsolutePage = 1;
			}

			if (_AbsolutePage > _TotalPages)
			{
				_AbsolutePage = _TotalPages;
			}

			Wxf _Wxf  = new Wxf();

			_Wxf.XDeclareStart();
			foreach(AttributeMapping _AttributeMapping in oZZQLite.AttributeMappings)
			{
				if (_AttributeMapping.STATUS_CODE != "HIDDEN")
				{
					_Wxf.XDeclare(_AttributeMapping);
				}
			}
			_Wxf.XDeclareFinal();

			_Wxf.Variable("ZZPageSize",_PageSize);
			_Wxf.Variable("ZZTotalPage",_TotalPages);
			_Wxf.Variable("USQL_CODE","QID02001B");

			while (!RsZUPRGDTM.EOF)
			{
				string cPickUpData   = "";
				object cValue;

				_Wxf.AddNew();
				int i=0;
				foreach(AttributeMapping _AttributeMapping in oZZQLite.AttributeMappings)
				{

					cValue = RsZUPRGDTM[i];

					if (_AttributeMapping.STATUS_CODE != "HIDDEN")
					{
						_Wxf[_AttributeMapping.Name] = cValue;
					}

					if (_AttributeMapping.PrimaryKey)
					{
						if (cPickUpData != "")
						{
							cPickUpData = cPickUpData + "|";
						}
						cPickUpData = cPickUpData + ZZGeneral.Base64Encoder(_AttributeMapping.ColumnName + "=" + cValue);
					}
					i++;
				}

				_Wxf.PK_DATA   = cPickUpData;
				_Wxf.Update();

				RsZUPRGDTM.MoveNext();
			}
			RsZUPRGDTM.Close();

			_Entity.Wxf=_Wxf;

			return _Entity;
		}

		public UID02001BE Retrieves(VContexts _VContexts,UID02001BE _Entity)
		{
			if (_DocsPermission.AccessRight("UID02001B", this.ZZLOGON_ID, "ACCESS")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02001B","NotAccessRight",this.ZZLOGON_ID));
				return _Entity;
			}

			List<UID02001BE> _New_Entitys=new List<UID02001BE>();


			CriteriaRetrieve objRetrieveZUPRGDTMEntity=new CriteriaRetrieve(typeof(ZUPRGDTMEntity));

			objRetrieveZUPRGDTMEntity.Top=1;
			objCondition=objRetrieveZUPRGDTMEntity.NewCondition();

			bool ZZOrderByAsc=_Setting.getBoolean("ZZOrderByAsc","YES");
			switch (_VContexts.getValue("UDEF_CODE"))
			{
				case "MF":
					{
						objRetrieveZUPRGDTMEntity.OrderBy("UID_DT",ZZOrderByAsc);
						break;
					}
				case "ML":
					{
						objRetrieveZUPRGDTMEntity.OrderBy("UID_DT",!ZZOrderByAsc);
						break;
					}
				case "MN":
					{
						if (ZZOrderByAsc)
						{
							objCondition.GreaterThan("UID_DT",_Entity.UID_DT);
						}else{
							objCondition.LessThan("UID_DT",_Entity.UID_DT);
						}
						objRetrieveZUPRGDTMEntity.OrderBy("UID_DT",ZZOrderByAsc);
						break;
					}
				case "MP":
					{
						if (ZZOrderByAsc)
						{
							objCondition.LessThan("UID_DT",_Entity.UID_DT);
						}else{
							objCondition.GreaterThan("UID_DT",_Entity.UID_DT);
						}
						objRetrieveZUPRGDTMEntity.OrderBy("UID_DT",!ZZOrderByAsc);
						break;
					}
				case "CANCEL":
					{
						objRetrieveZUPRGDTMEntity.OrderBy("UID_DT",ZZOrderByAsc);
						_Entity.DATA_STATUS_CODE="Verified";
						break;
					}
				case "LOOKUP":
					{
						objRetrieveZUPRGDTMEntity.OrderBy("UID_DT",ZZOrderByAsc);
						break;
					}

				case "ACTIVETAB":
					{
								if (_Entity.UID_DT != "")
								{
									objCondition.Equals("UID_DT", _Entity.UID_DT);
								}

						objRetrieveZUPRGDTMEntity.OrderBy("UID_DT",ZZOrderByAsc);
						break;
					}

				case "READ":
					{
									objCondition.Equals("UID_DT", _Entity.UID_DT);
						break;
					}					
				default:
					{
								if (_Entity.UID_DT != "")
								{
									objCondition.Equals("UID_DT", _Entity.UID_DT);
								}
						objRetrieveZUPRGDTMEntity.OrderBy("UID_DT",ZZOrderByAsc);

						break;
					}
			}
			
			//filter by quick look
			string _LOOUP_COLUMN_NAME=";ZUPRGDTM.UID_CODE;ZUPRGDTM.UID_DT;ZUPRGDTM.TABLE_NAME;ZUPRGDTM.COLUMN_NAME;ZUPRGDTM.LABEL_ID;ZUPRGDTM.BASE_COLUMN_NAME;ZUPRGDTM.BASE_TABLE_NAME;ZUPRGDTM.LNK_COLUMN_NAME;ZUPRGDTM.OPT_CODE;ZUPRGDTM.RELY_VERFY;ZUPRGDTM.RELY_DEFAULT;ZUPRGDTM.RELY_VIEW";
			string LOOKUP_DATA=_VContexts.getValue("LOOKUP_DATA");
			_LOOUP_COLUMN_NAME=_LOOUP_COLUMN_NAME+";";
			if (LOOKUP_DATA!="")
			{
				foreach (string s in _LOOUP_COLUMN_NAME.Split(new char[] { ';' }))
				{
					if (!string.IsNullOrEmpty(s))
					{
						Condition OrCondition=objCondition.NewCondition();
						OrCondition.Expression(s,LOOKUP_DATA);
					}
				}
			}
			
			//filter by filters


			//filter by AccessConditions

				_DataPermission.AccessCondition(objCondition);

						if (!Util.IsNullOrEmpty(_Entity.UID_CODE))
						{
							objCondition.Equals("UID_CODE", _Entity.UID_CODE);
						}


			ZUPRGDTMEntity objZUPRGDTMs=(ZUPRGDTMEntity)this.Trans.RetrieveEntity(objRetrieveZUPRGDTMEntity);
            
			ZZTrace.Debug(ZFILE_NAME,"SELECT="+objRetrieveZUPRGDTMEntity.SELECTClause);

			if (objZUPRGDTMs==null && _VContexts.getValue("UDEF_CODE")=="MN")
			{
				_Entity.Exceptions.Add(new LogisticException("UID_DT","DataEOF"));
				return _Entity;
			}else if (objZUPRGDTMs==null && _VContexts.getValue("UDEF_CODE")=="MP")
			{
				_Entity.Exceptions.Add(new LogisticException("UID_DT","DataBOF"));
				return _Entity;
			}else if (objZUPRGDTMs==null && (_VContexts.getValue("UDEF_CODE")=="MF" || _VContexts.getValue("UDEF_CODE")=="ML") )
			{
				_Entity.Exceptions.Add(new LogisticException("UID_DT","DataBlank"));
				return _Entity;
			}else if (objZUPRGDTMs==null && _VContexts.getValue("UDEF_CODE")=="LOOKUP")
			{
				/*
				 *if (this.COLUMN_REFX.IndexOf(";")>=0)
				 *{
				 *}else{
				 *    string _T_COLUMN_REFX=this.COLUMN_REFX;
				 *    if (_T_COLUMN_REFX.IndexOf('.') > 0)
				 *    {
				 *        string[] s = _T_COLUMN_REFX.Split(new char[] { '.' });
				 *        _T_COLUMN_REFX=s[1];
				 *    }
				 *    _Entity.Exceptions.Add(new LogisticException(_T_COLUMN_REFX,"DataNotFd","DataBlank"));
				 *    return _Entity;
				 *}
				 */
			}else if (objZUPRGDTMs==null && _VContexts.getValue("UDEF_CODE")=="ACTIVETAB")
			{
				//throw new LogisticException("UID_DT","Activetab","");
				//this.Default(_Entity);
			}else if (objZUPRGDTMs==null)
			{
				//this.Default(_Entity);
			}else{
				ZUPRGDTMEntity objZUPRGDTMEntity = objZUPRGDTMs;
					ZZTrace.MUKI_DEBUG_LOG=true;
						_Entity.UID_CODE=objZUPRGDTMEntity.UID_CODE;
						_Entity.UID_DT=objZUPRGDTMEntity.UID_DT;
						_Entity.SERIAL_NO=objZUPRGDTMEntity.SERIAL_NO;
						_Entity.TABLE_NAME=objZUPRGDTMEntity.TABLE_NAME;
						_Entity.COLUMN_NAME=objZUPRGDTMEntity.COLUMN_NAME;
						_Entity.COLUMN_NULLABLE=objZUPRGDTMEntity.COLUMN_NULLABLE;
						_Entity.LABEL_ID=objZUPRGDTMEntity.LABEL_ID;
						_Entity.BASE_COLUMN_NAME=objZUPRGDTMEntity.BASE_COLUMN_NAME;
						_Entity.BASE_TABLE_NAME=objZUPRGDTMEntity.BASE_TABLE_NAME;
						_Entity.LNK_COLUMN_NAME=objZUPRGDTMEntity.LNK_COLUMN_NAME;
						_Entity.OPT_CODE=objZUPRGDTMEntity.OPT_CODE;
						_Entity.RELY_VERFY=objZUPRGDTMEntity.RELY_VERFY;
						_Entity.RELY_DEFAULT=objZUPRGDTMEntity.RELY_DEFAULT;
						_Entity.RELY_VIEW=objZUPRGDTMEntity.RELY_VIEW;
						_Entity.NEW_LINE=objZUPRGDTMEntity.NEW_LINE;
						_Entity.NEW_CELL=objZUPRGDTMEntity.NEW_CELL;
						_Entity.ROWS_SPAN=objZUPRGDTMEntity.ROWS_SPAN;
						_Entity.COLS_SPAN=objZUPRGDTMEntity.COLS_SPAN;
						_Entity.ACTIVE_BIT=objZUPRGDTMEntity.ACTIVE_BIT;
						_Entity.LINE_REVEAL=objZUPRGDTMEntity.LINE_REVEAL;

				_Entity.UZADD_DATE     =_Convert.ToDateTimeString(objZUPRGDTMEntity.ZADD_DATE);
_Entity.UZADD_USER     =objZUPRGDTMEntity.ZADD_USER;
_Entity.UZCHG_DATE     =_Convert.ToDateTimeString(objZUPRGDTMEntity.ZCHG_DATE);
_Entity.UZCHG_USER     =objZUPRGDTMEntity.ZCHG_USER;
_Entity.USECURITY_CODE1=objZUPRGDTMEntity.ZSECURITY_CODE1;
_Entity.USECURITY_CODE2=objZUPRGDTMEntity.ZSECURITY_CODE2;
_Entity.USECURITY_CODE3=objZUPRGDTMEntity.ZSECURITY_CODE3;
_Entity.USECURITY_CODE4=objZUPRGDTMEntity.ZSECURITY_CODE4;
_Entity.USECURITY_CODE5=objZUPRGDTMEntity.ZSECURITY_CODE5;
_Entity.USECURITY_CODE6=objZUPRGDTMEntity.ZSECURITY_CODE6;



					_Entity.DATA_STATUS_CODE="Verified";
			}

	QueryRows RsZULABELM = new QueryRows(this.Trans);
	RsZULABELM.CommandText = "SELECT * FROM ZULABELM WHERE LABEL_ID='"+_Entity.LABEL_ID+"'";
	RsZULABELM.Open();
	while (!RsZULABELM.EOF)
	{
		if (RsZULABELM.getValue("LANG_CODE")=="CHS"){
			_Entity.UID_CHS_NAME=RsZULABELM.getValue("LANG_NAME");
		}else if (RsZULABELM.getValue("LANG_CODE")=="ENG"){
			_Entity.UID_ENG_NAME=RsZULABELM.getValue("LANG_NAME");
		}
		RsZULABELM.MoveNext();
	}
	RsZULABELM.Close();


				return _Entity;
		}

		public  UID02001BE AddNew(UID02001BE _Entity)
		{
			
			ZZTrace.Debug(ZFILE_NAME,"ZZLOGON_ID="+this.ZZLOGON_ID);

			if (_DocsPermission.AccessRight("UID02001B", this.ZZLOGON_ID, "ADDNEW")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02001B","NotAddNewRight",this.ZZLOGON_ID));
				return _Entity;
			}

			_Entity.ATVT_CODE="ADDNEW";


				_Entity=this.Verify(_Entity);
			if (_Entity.Exceptions.Count>0)
			{
				return _Entity;
			}
			ZUPRGDTMEntity objAddNewEntity=new ZUPRGDTMEntity();

			ZZHDKeyLib objZZHDKeyLib=new ZZHDKeyLib();
			objZZHDKeyLib.Trans=this.Trans;
							objZZHDKeyLib.LNK_COLUMN("UID_CODE",_Entity.UID_CODE);
							objZZHDKeyLib.LNK_COLUMN_VALUE=_Entity.UID_CODE;

				objZZHDKeyLib.PREFIX_CODE="UID02001B";
				objZZHDKeyLib.getSerial("ZUPRGDTM");
				int XSERIAL_NO=objZZHDKeyLib.PREFIX_CN;

				_Entity.UID_DT=objZZHDKeyLib.DT_NO;

					objAddNewEntity.UID_DT=_Entity.UID_DT;

			this.Trans.RetrieveEntity(objAddNewEntity);

			if (objAddNewEntity.Verified)
			{
				_Entity.Exceptions.Add(new LogisticException("UID_DT","DataExist",_Entity.UID_DT));
				return _Entity;
			}else{

				objAddNewEntity.ZADD_DATE = DateTime.Now;
				objAddNewEntity.ZADD_USER = this.ZZLOGON_ID;

				_DataPermission.TABLE_NAME="ZUPRGDTM";
				_DataPermission.DataAccessRight();
				objAddNewEntity.ZSECURITY_CODE1 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE1");
				objAddNewEntity.ZSECURITY_CODE2 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE2");
				objAddNewEntity.ZSECURITY_CODE3 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE3");
				objAddNewEntity.ZSECURITY_CODE4 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE4");
				objAddNewEntity.ZSECURITY_CODE5 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE5");
				objAddNewEntity.ZSECURITY_CODE6 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE6");
				objAddNewEntity.ZSECURITY_RIGHT = _DataPermission.ACCESS_AUTH.getInteger("SECURITY_RIGHT");

				_Entity.UZADD_DATE     =_Convert.ToDateTimeString(objAddNewEntity.ZADD_DATE);
_Entity.UZADD_USER     =objAddNewEntity.ZADD_USER;
_Entity.UZCHG_DATE     =_Convert.ToDateTimeString(objAddNewEntity.ZCHG_DATE);
_Entity.UZCHG_USER     =objAddNewEntity.ZCHG_USER;
_Entity.USECURITY_CODE1=objAddNewEntity.ZSECURITY_CODE1;
_Entity.USECURITY_CODE2=objAddNewEntity.ZSECURITY_CODE2;
_Entity.USECURITY_CODE3=objAddNewEntity.ZSECURITY_CODE3;
_Entity.USECURITY_CODE4=objAddNewEntity.ZSECURITY_CODE4;
_Entity.USECURITY_CODE5=objAddNewEntity.ZSECURITY_CODE5;
_Entity.USECURITY_CODE6=objAddNewEntity.ZSECURITY_CODE6;


	if (_Entity.UID_ENG_NAME!=""){
		ZULABELMEntity ENG_AddNewEntity = new ZULABELMEntity();
		ENG_AddNewEntity.UID_CODE  = "Default";
		ENG_AddNewEntity.LANG_CODE = "ENG";
		ENG_AddNewEntity.LABEL_ID  = _Entity.LABEL_ID;

		Trans.RetrieveEntity(ENG_AddNewEntity);

		if (!ENG_AddNewEntity.Verified)
		{
			ENG_AddNewEntity.UID_CODE   = "Default";
			ENG_AddNewEntity.LANG_CODE   = "ENG";
			ENG_AddNewEntity.LABEL_ID    = _Entity.LABEL_ID;
		}
		ENG_AddNewEntity.LANG_NAME   = _Entity.UID_ENG_NAME;
		Trans.SaveChanges(ENG_AddNewEntity);
	}
	if (_Entity.UID_CHS_NAME!=""){
		ZULABELMEntity CHS_AddNewEntity = new ZULABELMEntity();
		CHS_AddNewEntity.UID_CODE  = "Default";
		CHS_AddNewEntity.LANG_CODE = "CHS";
		CHS_AddNewEntity.LABEL_ID  = _Entity.LABEL_ID;

		Trans.RetrieveEntity(CHS_AddNewEntity);

		if (!CHS_AddNewEntity.Verified)
		{
			CHS_AddNewEntity.UID_CODE   = "Default";
			CHS_AddNewEntity.LANG_CODE   = "CHS";
			CHS_AddNewEntity.LABEL_ID    = _Entity.LABEL_ID;
		}
		CHS_AddNewEntity.LANG_NAME   = _Entity.UID_CHS_NAME;
		Trans.SaveChanges(CHS_AddNewEntity);
	}




							objAddNewEntity.UID_CODE=_Entity.UID_CODE;
							objAddNewEntity.UID_DT=_Entity.UID_DT;
							objAddNewEntity.SERIAL_NO=_Entity.SERIAL_NO;
							objAddNewEntity.TABLE_NAME=_Entity.TABLE_NAME;
							objAddNewEntity.COLUMN_NAME=_Entity.COLUMN_NAME;
							objAddNewEntity.COLUMN_NULLABLE=_Entity.COLUMN_NULLABLE;
							objAddNewEntity.LABEL_ID=_Entity.LABEL_ID;
							objAddNewEntity.BASE_COLUMN_NAME=_Entity.BASE_COLUMN_NAME;
							objAddNewEntity.BASE_TABLE_NAME=_Entity.BASE_TABLE_NAME;
							objAddNewEntity.LNK_COLUMN_NAME=_Entity.LNK_COLUMN_NAME;
							objAddNewEntity.OPT_CODE=_Entity.OPT_CODE;
							objAddNewEntity.RELY_VERFY=_Entity.RELY_VERFY;
							objAddNewEntity.RELY_DEFAULT=_Entity.RELY_DEFAULT;
							objAddNewEntity.RELY_VIEW=_Entity.RELY_VIEW;
							objAddNewEntity.NEW_LINE=_Entity.NEW_LINE;
							objAddNewEntity.NEW_CELL=_Entity.NEW_CELL;
							objAddNewEntity.ROWS_SPAN=_Entity.ROWS_SPAN;
							objAddNewEntity.COLS_SPAN=_Entity.COLS_SPAN;
							objAddNewEntity.ACTIVE_BIT=_Entity.ACTIVE_BIT;
							objAddNewEntity.LINE_REVEAL=_Entity.LINE_REVEAL;
					objAddNewEntity.SERIAL_NO=XSERIAL_NO;
					_Entity.SERIAL_NO=XSERIAL_NO;
			}


				this.Trans.SaveChanges(objAddNewEntity);


				_Entity.DATA_STATUS_CODE="Verified";
			return _Entity;
		}

		public  UID02001BE Update(UID02001BE _Entity)
		{

			if (_DocsPermission.AccessRight("UID02001B", this.ZZLOGON_ID, "UPDATE")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02001B","NotUpdateRight",this.ZZLOGON_ID));
				return _Entity;
			}
			_Entity.ATVT_CODE="UPDATE";


						if (_Entity.UID_DT=="")
						{
							_Entity.Exceptions.Add(new LogisticException("UID_DT","DataMiss",""));
						}

			_Entity=this.Verify(_Entity);
			ZZTrace.Debug(ZFILE_NAME,"_Entity.Exceptions.Count="+_Entity.Exceptions.Count);
			if (_Entity.Exceptions.Count>0)
			{
				return _Entity;
			}

					//ZZTrace.Debug(ZFILE_NAME,"_Entity.UID_CODE="+_Entity.UID_CODE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.UID_DT="+_Entity.UID_DT);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.SERIAL_NO="+_Entity.SERIAL_NO);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.TABLE_NAME="+_Entity.TABLE_NAME);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.COLUMN_NAME="+_Entity.COLUMN_NAME);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.COLUMN_NULLABLE="+_Entity.COLUMN_NULLABLE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.LABEL_ID="+_Entity.LABEL_ID);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.BASE_COLUMN_NAME="+_Entity.BASE_COLUMN_NAME);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.BASE_TABLE_NAME="+_Entity.BASE_TABLE_NAME);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.LNK_COLUMN_NAME="+_Entity.LNK_COLUMN_NAME);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.OPT_CODE="+_Entity.OPT_CODE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.RELY_VERFY="+_Entity.RELY_VERFY);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.RELY_DEFAULT="+_Entity.RELY_DEFAULT);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.RELY_VIEW="+_Entity.RELY_VIEW);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.NEW_LINE="+_Entity.NEW_LINE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.NEW_CELL="+_Entity.NEW_CELL);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.ROWS_SPAN="+_Entity.ROWS_SPAN);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.COLS_SPAN="+_Entity.COLS_SPAN);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.ACTIVE_BIT="+_Entity.ACTIVE_BIT);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.LINE_REVEAL="+_Entity.LINE_REVEAL);

			objRetrieveEntity=new CriteriaRetrieve(typeof(ZUPRGDTMEntity));
			objCondition=objRetrieveEntity.NewCondition();

					objCondition.Equals("UID_DT", _Entity.UID_DT);


			_DataPermission.UpdateCondition(objCondition);


			EntityObject objEntityObject=this.Trans.RetrieveEntity(objRetrieveEntity);

			if (objEntityObject != null)
			{
				ZUPRGDTMEntity objUpdateEntity=(ZUPRGDTMEntity)objEntityObject;
				if (objUpdateEntity.Verified)
				{
					objUpdateEntity.ZCHG_DATE=DateTime.Now;
					objUpdateEntity.ZCHG_USER=this.ZZLOGON_ID;

	if (_Entity.UID_ENG_NAME!=""){
		ZULABELMEntity ENG_AddNewEntity = new ZULABELMEntity();
		ENG_AddNewEntity.UID_CODE  = "Default";
		ENG_AddNewEntity.LANG_CODE = "ENG";
		ENG_AddNewEntity.LABEL_ID  = _Entity.LABEL_ID;

		Trans.RetrieveEntity(ENG_AddNewEntity);

		if (!ENG_AddNewEntity.Verified)
		{
			ENG_AddNewEntity.UID_CODE   = "Default";
			ENG_AddNewEntity.LANG_CODE   = "ENG";
			ENG_AddNewEntity.LABEL_ID    = _Entity.LABEL_ID;
		}
		ENG_AddNewEntity.LANG_NAME   = _Entity.UID_ENG_NAME;
		Trans.SaveChanges(ENG_AddNewEntity);
	}
	if (_Entity.UID_CHS_NAME!=""){
		ZULABELMEntity CHS_AddNewEntity = new ZULABELMEntity();
		CHS_AddNewEntity.UID_CODE  = "Default";
		CHS_AddNewEntity.LANG_CODE = "CHS";
		CHS_AddNewEntity.LABEL_ID  = _Entity.LABEL_ID;

		Trans.RetrieveEntity(CHS_AddNewEntity);

		if (!CHS_AddNewEntity.Verified)
		{
			CHS_AddNewEntity.UID_CODE   = "Default";
			CHS_AddNewEntity.LANG_CODE   = "CHS";
			CHS_AddNewEntity.LABEL_ID    = _Entity.LABEL_ID;
		}
		CHS_AddNewEntity.LANG_NAME   = _Entity.UID_CHS_NAME;
		Trans.SaveChanges(CHS_AddNewEntity);
	}



								objUpdateEntity.UID_CODE=_Entity.UID_CODE;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE UID_CODE="+objUpdateEntity.UID_CODE+" "+_Entity.UID_CODE);
								
								objUpdateEntity.UID_DT=_Entity.UID_DT;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE UID_DT="+objUpdateEntity.UID_DT+" "+_Entity.UID_DT);
								
								objUpdateEntity.SERIAL_NO=_Entity.SERIAL_NO;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE SERIAL_NO="+objUpdateEntity.SERIAL_NO+" "+_Entity.SERIAL_NO);
								
								objUpdateEntity.TABLE_NAME=_Entity.TABLE_NAME;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE TABLE_NAME="+objUpdateEntity.TABLE_NAME+" "+_Entity.TABLE_NAME);
								
								objUpdateEntity.COLUMN_NAME=_Entity.COLUMN_NAME;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE COLUMN_NAME="+objUpdateEntity.COLUMN_NAME+" "+_Entity.COLUMN_NAME);
								
								objUpdateEntity.COLUMN_NULLABLE=_Entity.COLUMN_NULLABLE;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE COLUMN_NULLABLE="+objUpdateEntity.COLUMN_NULLABLE+" "+_Entity.COLUMN_NULLABLE);
								
								objUpdateEntity.LABEL_ID=_Entity.LABEL_ID;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE LABEL_ID="+objUpdateEntity.LABEL_ID+" "+_Entity.LABEL_ID);
								
								objUpdateEntity.BASE_COLUMN_NAME=_Entity.BASE_COLUMN_NAME;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE BASE_COLUMN_NAME="+objUpdateEntity.BASE_COLUMN_NAME+" "+_Entity.BASE_COLUMN_NAME);
								
								objUpdateEntity.BASE_TABLE_NAME=_Entity.BASE_TABLE_NAME;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE BASE_TABLE_NAME="+objUpdateEntity.BASE_TABLE_NAME+" "+_Entity.BASE_TABLE_NAME);
								
								objUpdateEntity.LNK_COLUMN_NAME=_Entity.LNK_COLUMN_NAME;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE LNK_COLUMN_NAME="+objUpdateEntity.LNK_COLUMN_NAME+" "+_Entity.LNK_COLUMN_NAME);
								
								objUpdateEntity.OPT_CODE=_Entity.OPT_CODE;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE OPT_CODE="+objUpdateEntity.OPT_CODE+" "+_Entity.OPT_CODE);
								
								objUpdateEntity.RELY_VERFY=_Entity.RELY_VERFY;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE RELY_VERFY="+objUpdateEntity.RELY_VERFY+" "+_Entity.RELY_VERFY);
								
								objUpdateEntity.RELY_DEFAULT=_Entity.RELY_DEFAULT;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE RELY_DEFAULT="+objUpdateEntity.RELY_DEFAULT+" "+_Entity.RELY_DEFAULT);
								
								objUpdateEntity.RELY_VIEW=_Entity.RELY_VIEW;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE RELY_VIEW="+objUpdateEntity.RELY_VIEW+" "+_Entity.RELY_VIEW);
								
								objUpdateEntity.NEW_LINE=_Entity.NEW_LINE;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE NEW_LINE="+objUpdateEntity.NEW_LINE+" "+_Entity.NEW_LINE);
								
								objUpdateEntity.NEW_CELL=_Entity.NEW_CELL;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE NEW_CELL="+objUpdateEntity.NEW_CELL+" "+_Entity.NEW_CELL);
								
								objUpdateEntity.ROWS_SPAN=_Entity.ROWS_SPAN;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE ROWS_SPAN="+objUpdateEntity.ROWS_SPAN+" "+_Entity.ROWS_SPAN);
								
								objUpdateEntity.COLS_SPAN=_Entity.COLS_SPAN;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE COLS_SPAN="+objUpdateEntity.COLS_SPAN+" "+_Entity.COLS_SPAN);
								
								objUpdateEntity.ACTIVE_BIT=_Entity.ACTIVE_BIT;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE ACTIVE_BIT="+objUpdateEntity.ACTIVE_BIT+" "+_Entity.ACTIVE_BIT);
								
								objUpdateEntity.LINE_REVEAL=_Entity.LINE_REVEAL;

								//ZZTrace.Debug(ZFILE_NAME,"UPDATE LINE_REVEAL="+objUpdateEntity.LINE_REVEAL+" "+_Entity.LINE_REVEAL);
								

						this.Trans.SaveChanges(objUpdateEntity);


						_Entity.UZADD_DATE     =_Convert.ToDateTimeString(objUpdateEntity.ZADD_DATE);
_Entity.UZADD_USER     =objUpdateEntity.ZADD_USER;
_Entity.UZCHG_DATE     =_Convert.ToDateTimeString(objUpdateEntity.ZCHG_DATE);
_Entity.UZCHG_USER     =objUpdateEntity.ZCHG_USER;
_Entity.USECURITY_CODE1=objUpdateEntity.ZSECURITY_CODE1;
_Entity.USECURITY_CODE2=objUpdateEntity.ZSECURITY_CODE2;
_Entity.USECURITY_CODE3=objUpdateEntity.ZSECURITY_CODE3;
_Entity.USECURITY_CODE4=objUpdateEntity.ZSECURITY_CODE4;
_Entity.USECURITY_CODE5=objUpdateEntity.ZSECURITY_CODE5;
_Entity.USECURITY_CODE6=objUpdateEntity.ZSECURITY_CODE6;


					_Entity.DATA_STATUS_CODE="Verified";

				}else{
					_Entity.Exceptions.Add(new LogisticException("UID_DT","DataNotFd"));
				}
			}else{
				_Entity.Exceptions.Add(new LogisticException("UID_DT","DataNotFd"));
			}
			return _Entity;
		}

		public  UID02001BE Delete(UID02001BE _Entity)
		{
			if (_DocsPermission.AccessRight("UID02001B", this.ZZLOGON_ID, "DELETE")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02001B","NotDeleteRight"));
				return _Entity;
			}
			_Entity.ATVT_CODE="DELETE";

					if (_Entity.UID_DT=="")
					{
						_Entity.Exceptions.Add(new LogisticException("UID_DT","DataMiss"));
						return _Entity;
					}


				objRetrieveEntity=new CriteriaRetrieve(typeof(ZUPRGDTMEntity));
			objCondition=objRetrieveEntity.NewCondition();

					objCondition.Equals("UID_DT", _Entity.UID_DT);


			_DataPermission.DeleteCondition(objCondition);

			EntityObject objDeleteEntity=this.Trans.RetrieveEntity(objRetrieveEntity);

			if (objDeleteEntity != null)
			{
				if (objDeleteEntity.Verified)
				{
						this.Trans.DeleteEntity(objDeleteEntity);
				}else{
					_Entity.Exceptions.Add(new LogisticException("UID_DT","DataNotFd"));
					return _Entity;
				}
			}else{
				_Entity.Exceptions.Add(new LogisticException("UID_DT","DataNotFd"));
			}
			return _Entity;

		}

		public UID02001BE UserDefine(VContexts cMethod,UID02001BE _Entity)
		{
				return _Entity;
		}


#region Field
		private CriteriaRetrieve objRetrieveEntity;
		private List<EntityObject>  objEntityContainer;
		private Condition        objCondition;
		private ZZDefault        objDefault      = new ZZDefault();
		private ZZDataPermission _DataPermission = new ZZDataPermission();
		private ZZDocsPermission _DocsPermission = new ZZDocsPermission();
		private ZZSetting        _Setting        = new ZZSetting();
		private XConvert         _Convert        = new XConvert();

#endregion


		const  string ZFILE_NAME   ="UID02001BM";
		const  string MUKI_EDITION ="MIN";


	}
}
